package com.woniu.his.service.impl;

import com.woniu.his.dao.*;
import com.woniu.his.entity.*;
import com.woniu.his.service.IOperationInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.service.IPayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TATO
 * @since 2019-11-25
 */
@Service
public class OperationInfoServiceImpl extends ServiceImpl<OperationInfoMapper, OperationInfo> implements IOperationInfoService {
    @Autowired
    private OperationInfoMapper operationInfoMapper;

    @Autowired
    private PatientInfoMapper patientInfoMapper;

    @Autowired
    private MedicalInfoMapper medicalInfoMapper;

    @Autowired
    private DrugInfoMapper drugInfoMapper;
    @Autowired
    private EquipmentInfoMapper equipmentInfoMapper;

    @Autowired
    private IPayInfoService payInfoService;

    @Autowired
    private DocterInfoMapper docterInfoMapper;

    @Override
    public OperationDetailInfo getOperationInfoByOperationId(int operationId) {
        return operationInfoMapper.getOperationInfoByOperationId(operationId);
    }

    @Override
    public List<OperationDetailInfo> getOperationInfoByMedicalId(Integer medicalId) {
        return operationInfoMapper.getOperationInfoByMedicalId(medicalId);
    }

    @Override
    public List<OperationDetailInfo> getOperationInfoList() {
        return operationInfoMapper.getOperationInfoList();
    }

    @Override
    public int deleteOperationInfoByOperationId(int operationId) {
        return operationInfoMapper.deleteOperationInfoByOperationId(operationId);
    }

    @Override
    public int updateOperationPayByOperationId(int operationId, int isPay) {
        isPay = isPay == 0 ? 1 : 0;
        return operationInfoMapper.updateOperationPayByOperationId(operationId, isPay);
    }

    @Override
    public int updateOperationActionByOperationId(int operationId, int isAction) {
        isAction = isAction == 0 ? 1 : 0;
        return operationInfoMapper.updateOperationActionByOperationId(operationId, isAction);
    }
    @Transactional
    @Override
    public int updateOperationInfoByOperationId(int medicalId,int operationId, String operationType, String operationName, int docterId, Date operationTime) {
        PayInfo payInfo = new PayInfo();
        payInfo.setMedicalId(medicalId);
        payInfo.setPayProject(operationType);
        Integer count = 1;
        System.out.println(count);
        payInfo.setCount(count);
        BigDecimal price;
        if("微创手术".equals(operationType)){
            price = new BigDecimal(2200);
        }else if("小型手术".equals(operationType)){
            price = new BigDecimal(5500);
        }else if("中型手术".equals(operationType)){
            price = new BigDecimal(15000);
        }else{
            price = new BigDecimal(50000);
        }
        payInfo.setPrice(price);
        BigDecimal totalPrice = price.multiply(new BigDecimal(count));
        totalPrice = totalPrice.setScale(2, BigDecimal.ROUND_HALF_UP);
        payInfo.setTotalPrice(totalPrice);
        payInfo.setPayTime(new Date());
        payInfoService.addPayInfo(payInfo);
        return operationInfoMapper.updateOperationInfoByOperationId(operationId, operationType, operationName, docterId, operationTime);
    }

    @Transactional
    @Override
    public OperationAllDetailInfo showPatientDetailByOperationId(int operationId,int medicalId) {
        OperationAllDetailInfo result = new OperationAllDetailInfo();
        MedicalDetailInfo medicalDetailInfo = medicalInfoMapper.getMedicalDetailInfoByMedicalId(medicalId);
        OperationDetailInfo operationInfo = operationInfoMapper.getOperationInfoByOperationId(operationId);
        List<MedicalDetailInfo> medicalInfos = medicalInfoMapper.getMedicalDetailInfoByPatientId(operationInfo.getPatientId());
        PatientInfo patientInfo = patientInfoMapper.selectPatientInfoByPatientId(operationInfo.getPatientId());
        result.setPatientName(patientInfo.getPatientName());
        result.setPatientSex(patientInfo.getPatientSex());
        result.setPatientOccu(patientInfo.getPatientOccu());
        result.setPatientTel(patientInfo.getPatientTel());
        result.setPatientRelatives(patientInfo.getPatientRelatives());
        result.setPatientRelativesTel(patientInfo.getPatientRelativesTel());
        result.setRegisterTime(patientInfo.getRegisterTime());
        result.setCurrentMedicalInfo(medicalDetailInfo);
        result.setMedicalInfos(medicalInfos);
        return result;
    }

    @Override
    public List<DrugInfo> getDrugInfo() {
        return drugInfoMapper.getDrugInfo();
    }

    @Override
    public List<EquipmentInfo> getEquipmentInfo() {
        return equipmentInfoMapper.getEquipmentInfo();
    }

    @Transactional
    @Override
    public String addPayInfo(List<Map<String, Object>> payList) {
        int medicalId = -1;
        for(int i=0;i< payList.size();i++){
            if(Integer.parseInt(payList.get(i).get("medicalId").toString()) != 0){
                medicalId = Integer.parseInt(payList.get(i).get("medicalId").toString());
            }
        }
        if(medicalId < 1){
            return "-999";
        }
        MedicalInfo medicalInfo = medicalInfoMapper.selectMedicalInfoByMedicalId(medicalId);
        if(medicalInfo == null){
            return "-9999";
        }
        for (Map<String, Object> map : payList) {
            if(Integer.parseInt(map.get("medicalId").toString()) !=0){
                continue;
            }
            PayInfo payInfo = new PayInfo();
            payInfo.setMedicalId(medicalId);
            payInfo.setPayProject(map.get("drugVal").toString());
            Integer count = Integer.parseInt(map.get("drugCount").toString());
            System.out.println(count);
            payInfo.setCount(count);
            BigDecimal price = new BigDecimal(map.get("drugPrice").toString());
            payInfo.setPrice(price);
            BigDecimal totalPrice = price.multiply(new BigDecimal(count));
            totalPrice = totalPrice.setScale(2, BigDecimal.ROUND_HALF_UP);
            payInfo.setTotalPrice(totalPrice);
            payInfo.setPayTime(new Date());
            payInfoService.addPayInfo(payInfo);
        }
        return  "1";
    }

    @Override
    public int updateOperationHandleByOperationId(int operationId, int isHandle) {
        isHandle = isHandle == 0 ? 1 : 0;
        return operationInfoMapper.updateOperationHandleByOperationId(operationId, isHandle);
    }

    @Transactional
    @Override
    public OperationPeopleInfo mainPageLoad() {
        OperationPeopleInfo peopleInfo = new OperationPeopleInfo();
        peopleInfo.setOperationCount(operationInfoMapper.getOperationInfoCount());
        peopleInfo.setOperationUnActionCount(operationInfoMapper.getOperationInfoUnActionCount());
        peopleInfo.setOperationDocterCount(docterInfoMapper.getOperationDocterCount(3));
        return peopleInfo;
    }

    @Override
    public List<OperationDetailInfo> getOperationInfoByAction() {
        return operationInfoMapper.getOperationInfoByAction();
    }
}
